/************************************************************************************/
/*  Program name CAPAPIMP_PAY_DOC.sql                                               */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER AP_CHECK_STOCKS_ALL_CT1 DISABLE;
ALTER TRIGGER AP_CHECK_STOCKS_ALL_CT2 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_PAY_DOC';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_CHECK_STOCKS;

	lv_Next_Seq_ID	NUMBER;
	lv_SOB_Name		VARCHAR2(20);
	lv_COA_ID		NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_CHECK_STOCKS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_CHECK_STOCKS_MAP VALUES
			(
			C1.CHECK_STOCK_ID,
			C1.BANK_ACCOUNT_ID,
			C1.NAME,
			lv_Next_Seq_ID,
			CAPBRIMP_MAPPING.GetBankACMapping(C1.BANK_ACCOUNT_ID),
			C1.NAME
			);

			--lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortName(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortNameOU(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(lv_SOB_Name);

			INSERT INTO AP_CHECK_STOCKS_ALL
			VALUES
			(
			lv_Next_Seq_ID,													--CHECK_STOCK_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),				--LAST_UPDATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),					--CREATED_BY,
			C1.NAME,
			CAPBRIMP_MAPPING.GetBankACMapping(C1.BANK_ACCOUNT_ID),			--BANK_ACCOUNT_ID,
			C1.DISBURSEMENT_TYPE_LOOKUP_CODE,
			CAPBRIMP_MAPPING.GetPayFormatMapping(C1.CHECK_FORMAT_ID),		--CHECK_FORMAT_ID,
			C1.LAST_DOCUMENT_NUM,
			C1.LAST_AVAILABLE_DOCUMENT_NUM,
			C1.NUM_SETUP_CHECKS,
			C1.AUTO_MEDIUM_ID,												--  ???
			C1.CHECK_RECON_GROUP_ID,										--  ???
			C1.SEQUENTIAL_FLAG,
			C1.INACTIVE_DATE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.FUTURE_PAY_CCID_D),		--FUTURE_PAY_CODE_COMBINATION_ID,
			C1.ATTRIBUTE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.DOC_CATEGORY_CODE,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),								--ORG_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BANK_CHAREGES_CCID_D),		--BANK_CHARGES_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BANK_ERRORS_CCID_D),		--BANK_ERRORS_CCID,
			C1.CLEARING_DAYS,
			C1.FUTURE_DATED_PAYMENTS_FLAG,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CASH_CLEARING_CCID_D),		--CASH_CLEARING_CCID,
			C1.TRANSMISSION_CODE
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AP_CHECK_STOCKS_ALL_CT1 ENABLE;
ALTER TRIGGER AP_CHECK_STOCKS_ALL_CT2 ENABLE;
